/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelAgregarNeumaticoReencauche.java
 *
 * Created on 15-jun-2012, 16:04:16
 */
package com.camachosanchez.gui;

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import javax.swing.TransferHandler;
import javax.swing.TransferHandler.TransferSupport;

/**
 *
 * @author Rolando
 */
public class PanelAgregarNeumaticoReencauche extends javax.swing.JPanel {

    private boolean drop;
    private EnvioReencauche dialogo;

    /** Creates new form PanelAgregarNeumaticoReencauche */
    public PanelAgregarNeumaticoReencauche() {
        initComponents();
        drop = false;
        setMaximumSize(new Dimension(450, 60));
        setMinimumSize(new Dimension(450, 60));
        setPreferredSize(new Dimension(450, 60));
        setSize(new Dimension(450, 60));
        this.setTransferHandler(new TransferHandler() {

            @Override
            public boolean canImport(TransferSupport support) {
                if (!support.isDrop()) {
                    return false;
                }
                if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    return false;
                }
                return true;
            }

            @Override
            public boolean importData(TransferSupport support) {
                if (!canImport(support)) {
                    return false;
                }
                String llanta_agregada;
                try {
                    llanta_agregada = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
                    String tokens[] = llanta_agregada.split("\t");
                    int id = Integer.parseInt(tokens[4]);
                    dialogo.agregarLlanta(id);
                } catch (UnsupportedFlavorException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
        });
        try {
            this.getDropTarget().addDropTargetListener(new DropTargetListener() {

                @Override
                public void dragOver(DropTargetDragEvent e) {
                    drop = true;
                    repaint();
                }

                @Override
                public void dragEnter(DropTargetDragEvent e) {
                    drop = true;
                    repaint();
                }

                @Override
                public void dragExit(DropTargetEvent e) {
                    drop = false;
                    repaint();
                }

                @Override
                public void drop(DropTargetDropEvent e) {
                    drop = false;
                    repaint();
                }

                @Override
                public void dropActionChanged(DropTargetDragEvent e) {
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public EnvioReencauche getDialogo() {
        return dialogo;
    }

    public void setDialogo(EnvioReencauche dialogo) {
        this.dialogo = dialogo;
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int w = this.getWidth();
        int h = this.getHeight();
        int round = 20;
        float dash[] = {2.0f};
        graphics.setColor(new Color(240, 240, 240));
        graphics.fillRect(0, 0, w, h);
        graphics.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));
        graphics.setColor(new Color(255, 255, 204));
        int border = 5;
        graphics.fillRoundRect(border, border, w - 2 * border, h - 2 * border, round, round);
        graphics.setColor(Color.black);
        graphics.drawRoundRect(border, border, w - 2 * border, h - 2 * border, round, round);
        String mensaje = "Arrastre un neumático aquí";
        Font old = graphics.getFont();
        Font fuente = new Font("Dialog", Font.PLAIN, 24);
        graphics.setFont(fuente);
        FontMetrics fm = g.getFontMetrics(fuente);
        int hFont = fm.getHeight();
        int wFont = fm.stringWidth(mensaje);
        graphics.setColor(new Color(100, 100, 100));
        graphics.drawString(mensaje, (w - wFont) / 2, (h + hFont / 2) / 2);
        graphics.setFont(old);
        if (drop) {
            graphics.setStroke(new BasicStroke(3f));
            graphics.setColor(new Color(51, 102, 204, 80));
            graphics.fillRoundRect(border, border, w - 2 * border, h - 2 * border, round, round);
        }
        graphics.setStroke(new BasicStroke());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setPreferredSize(new java.awt.Dimension(400, 104));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 380, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 104, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
